package com.kfy.progressbardemo.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kfy.progressbardemo.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ProgressBar progressBar1;
    ProgressBar progressBar2;
    ProgressBar progressBar3;
    ProgressBar progressBar4;
    TextView textView;
    Button button;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setView();
        setEvent();
    }

    private void setEvent() {
        new Thread(()->{
            try {
                progressBar4.setProgress(0);
                progressBar4.setSecondaryProgress(0);
                for (int i = 0; i<=100;i++){

                    progressBar4.incrementProgressBy(1);
                    progressBar4.incrementSecondaryProgressBy((int) (Math.random() * 3)+1);
                    int progress = progressBar4.getProgress();
                    int secondProgress = progressBar4.getSecondaryProgress();
                    int max = progressBar4.getMax();
                    Thread.sleep(500);
                    new Handler(Looper.getMainLooper()).post(() -> {
                        textView.setText(progress+"("+secondProgress+") / "+max);
                    });

                }
            }catch (Exception ignored){

            }
        }).start();
    }

    private void setView() {
        progressBar1 = findViewById(R.id.progressBar_circle_small);
        progressBar2 = findViewById(R.id.progressBar_circle);
        progressBar3 = findViewById(R.id.progressBar_circle_large);
        progressBar4 = findViewById(R.id.progressBar_horizontal);
        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button) {

            progressDialog = new ProgressDialog(MainActivity.this);
            //设置风格
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setTitle("对话框进度条");
            progressDialog.setIcon(R.mipmap.ic_launcher);
            //设置属性
            progressDialog.setMax(100);
            progressDialog.setProgress(0);
            progressDialog.setSecondaryProgress(0);
            progressDialog.setIndeterminate(false); //false 表示精确显示进度
            progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", (dialog, which) -> dialog.dismiss());
            progressDialog.setOnShowListener(dialog -> {
                new Thread(()-> {
                    for (int i = 0; i <= 100; i++) {
                        progressDialog.incrementProgressBy(1);
                        progressDialog.incrementSecondaryProgressBy((int) (Math.random() * 2) + 1);
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            });
            progressDialog.show();

        }
    }
}